#!usr/bin/env python  
# -*- coding:utf-8 _*-
""" 
@author:robot
@file: tmf_inspects.py 
@version:
@time: 2022/04/18 
"""
from TapClientCore.T02_tm_api.v2_api.v2_tm import v2tm as TM


class TMFInspects:
    @staticmethod
    def _get_inspects_by_id(filter=None):
        """
        查询Inspect任务
        :param filter: inspect id
        :return:
        """
        if filter:
            filter = {'where': {'id': filter}}
            return TM.get_inspects(filter)

    @staticmethod
    def _get_inspects_by_name(task_name):
        limit = 20
        skip = 0
        while True:
            filter = {
                "order": "createTime DESC",
                "limit": limit,
                "skip": skip,
                "where": {}}
            response = TM.get_inspects(filter)
            items = response.get('data').get('items')
            for one in items:
                if one.get('name') == task_name:
                    return one
            skip += limit
            if not items:
                break

    def inspects_check_name_repeat(self, inspect_name):
        """
        校验任务是否重复
        :param task_name:
        :return:
        """
        if self._get_inspects_by_name(inspect_name):
            return True

    def query_inspect_by_name(self, inspect_name):
        """
        通过名字查询 数据校验任务
        :param inspect_name:
        :return:
        """
        return self._get_inspects_by_name(inspect_name)

    @staticmethod
    def _delete_inspects(filter_id):
        """
        通过id删除inspects任务
        :param filter_id:
        :return:
        """
        return TM.delete_inspects_by_id(filter_id)

    def delete_inspects_by_name(self, name):
        """
        通过名字删除数据校验任务
        :param name:
        :return:
        """
        response = self.query_inspect_by_name(name)
        return self._delete_inspects(response.get('id'))

    @staticmethod
    def _post_inspects(payload):
        """
        创建数据校验任务
        :param payload:
        :return:
        """
        return TM.post_inspects(payload)

    @staticmethod
    def _get_inspects_find_one(inspect_id):
        data = {'where': {'id': inspect_id}}
        return TM.get_inspects_find_one(data)

    @staticmethod
    def _post_inspects_update(inspects_id, payload):
        where = {'id': inspects_id}
        return TM.post_inspects_update(where, payload)

    @staticmethod
    def _patch_inspects(data):
        """
        修改数据校验任务
        :param data:
        :return:
        """
        return TM.patch_inspects(data)

    def start_inspects(self, inspects_id):
        """
        开始数据校验
        :param inspects_id:
        :return:
        """
        data = {"status": "scheduling", "ping_time": 0, "scheduleTimes": 0, "byFirstCheckId": ""}
        self._post_inspects_update(inspects_id, data)

    def _create_inspects(self, data):
        """
        创建数据校验任务
        :param data: flowId|name|mode|row_count
        :return:
        """
        from TapClientCore.T03_api_body_templates.v2_api_body import INSPECTS_CREATE
        tmp_inspect = INSPECTS_CREATE.copy()
        tmp_inspect.update(data)
        return self._post_inspects(tmp_inspect)

    @staticmethod
    def init_inspect_node(data):
        """
        初始化校验节点
        :param data:
        :return:
        """
        from TapClientCore.T03_api_body_templates.v2_api_body import INSPECTS_TASK_NODE
        tmp_node = INSPECTS_TASK_NODE.copy()
        tmp_node.update(data)
        return tmp_node

    @staticmethod
    def init_inspect_task(data):
        """
        初始化数据校验task信息
        :param data: connectionId , connectionName, table, sortColumn, fields
        :return:
        """
        from TapClientCore.T03_api_body_templates.v2_api_body import INSPECTS_TASK
        tmp_task = INSPECTS_TASK.copy()
        tmp_task.update(data)
        return tmp_task

    def create_inspect(self, data):
        """
        创建数据校验任务集成
        :param data: source|target
        |task_node_setting(
        fullMatch, showAdvancedVerification, script, webScript, jsEngineName
        )
        |inspect_setting(
        flowId, name, mode, inspectMethod,
        )
        :return:
        """
        tasks = data.get('tasks')
        inspect_setting = data.get('inspect_setting')
        tasks_list = []
        for task in tasks:
            tasks_list.append(self.init_inspect_task(task))
        task_node = {
            'tasks': tasks_list,
            **inspect_setting

        }
        return self._create_inspects(task_node)
